<template>
	<view style="box-sizing: border-box; padding: 10px;">

		<view v-if="form.type==-1||form.type==0">
			<!-- -1为绑定 ！-1为修改 	-->
			<u-field :label-width="form.type!=-1?'200':'130'" :disabled="form.type!=-1?true:false" v-model="form.mobile"
				:label="form.type!=-1?'绑定的手机号':'手机号'" placeholder="请填写手机号">
			</u-field>

			<u-field v-model="form.code" label="验证码" placeholder="请填写验证码">
				<block v-if="showText">
					<u-button size="mini" slot="right" type="warning" @click="getCode">{{form.codeText}}</u-button>
				</block>
				<block v-else>
					<u-button size="mini" slot="right" type="warning">重新发送({{ second }})</u-button>
				</block>
				</block>

			</u-field>
			<u-field v-if="form.type!=-1" v-model="form.mobile2" label="新手机号" placeholder="请填写新手机号">
			</u-field>
			<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
			<view
				style="width: 95%; margin:40px auto; border-radius: 30px; background: #FA9700; color: #fff; line-height: 46px; text-align: center; "
				@click="bindPhone">
				<!-- {{form.type!=-1?'下一步':'绑定'}} -->
				修改手机号
			</view>
		</view>
		<!-- 1 修改密码 -->
		<u-form label-width="180" v-if="form.type==1" :model="form">
			<block v-if="userInfo.pwd">
				<u-form-item label="旧密码">
					<u-input v-model="form.name" placeholder="请填写旧密码" />
				</u-form-item>
			</block>
			<u-form-item label="新密码">
				<u-input placeholder="请填写新密码" v-model="form.intro" />
			</u-form-item>
			<u-form-item label="确认新密码">
				<u-input placeholder="请确认新密码" v-model="form.sex" />
			</u-form-item>
		</u-form>
		<!-- 2 修改支付密码 -->
		<u-form label-width="180" v-if="form.type==2" :model="form">
			<block v-if="userInfo.pay_pwd">
				<u-form-item label="旧密码">
					<u-input v-model="form.name" />
				</u-form-item>
			</block>
			<u-form-item label="新密码">
				<u-input v-model="form.intro" />
			</u-form-item>
			<u-form-item label="确认新密码">
				<u-input v-model="form.sex" />
			</u-form-item>
		</u-form>
		<view @click="wjmm" v-if="form.type==2||form.type==1" style="text-align: right; line-height: 40px;">忘记密码</view>
		<view v-if="form.type==2||form.type==1"
			style="width: 95%; margin:40px auto; border-radius: 30px; background: #FA9700; color: #fff; line-height: 46px; text-align: center; "
			@click="goMessage">
			确认修改
		</view>
	</view>
</template>

<script>
	import {
		setPwd,
		eidtBandPhone,
		getUserInfo,
		setPayPwd,
		sendSms
	} from "@/common/config/request.js"
	export default {
		data() {
			return {
				form: {
					codeText: ''
				},
				userInfo: "",
				second: 60, //默认60秒
				showText: true, //判断短信是否发送
				title:'',
			}
		},
		onLoad(e) {
			this.form.type = e.type
			// if(e.type==-1||e.type==0){
			// 	this.form.codeText="验证码"
			// }
			uni.setNavigationBarTitle({
				title: e.title
			})
			this.title=e.title
			this.form.mobile = uni.getStorageSync('mobile')
			this.getInfo()
		},
		methods: {
			getInfo() {
				// 用户详情
				getUserInfo({}).then(res => {
					if (res.data.code == 1) {
						this.userInfo = res.data.data
					}
				})
			},
			codeChange(text) {
				this.form.codeText = text;
			},
			getCode() {
				// if(this.$refs.uCode.canGetCode) {
				// 	// 模拟向后端请求验证码
				// 	uni.showLoading({
				// 		title: '正在获取验证码'
				// 	})
				// 	setTimeout(() => {
				// 		uni.hideLoading();
				// 		// 通知验证码组件内部开始倒计时
				// 		this.$refs.uCode.start();
				// 	}, 1000);
				// }else {
				// 	this.$u.toast('倒计时结束后再发送');
				// }
				this.yzphone().then((r) => {
					if (r == true) {
						sendSms({
							phone: this.form.mobile
						}).then((res) => {
							console.log(res)
							if (res.data.code == 1) {
								this.djs()
							}

						})
					}
				})
			},
			djs() {
				var that = this;
				var interval = setInterval(() => {
					that.showText = false;
					var times = that.second - 1;
					//that.second = times<10?'0'+times:times ;//小于10秒补 0
					that.second = times;
					console.log(times);
				}, 1000);
				setTimeout(() => {
					clearInterval(interval);
					that.second = 60;
					that.showText = true;
				}, 60000);
			},
			yzphone() {
				console.log(this.form.mobile)
				return new Promise((r, n) => {
					if (!this.form.mobile) {
						uni.showToast({
							title: '请输入手机号',
							icon: 'none'
						});
						return;
					}
					if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.form.mobile)) {
						uni.showToast({
							title: '请输入正确手机号',
							icon: 'none'
						});
						return;
					}
					r(true)
				})
			},
			// 修改绑定手机号
			bindPhone() {
				// console.log(this.form,'绑定手机号')
				var data = {
					phone: this.form.mobile2,
					code: this.form.code,
				}
				eidtBandPhone(data).then(res => {
					if (res.data.code == 1) {
						uni.showToast({
							icon: 'none',
							title: '修改成功'
						})
						this.form.mobile2 = ""
						this.form.code = ""
					}
				})
			},
			goMessage() {
				if (this.form.type == 1) {
					// 修改登录密码
					var data = {
						pwd: this.form.intro
					}
					if (!this.form.intro) {
						uni.showToast({
							icon: 'none',
							title: '新密码不成为空'
						})
					}
					if (this.form.intro == this.form.sex) {
						// console.log(data,'2222')
						setPwd(data).then(res => {
							if (res.data.code == 1) {
								var text
								if(this.title=='设置密码'){
									text='设置成功'
									this.getuserinfo.getuserinfo().then((r)=>{
										console.log('eeeeeeeee',r)
										if(r.code==1){
											if(r.data.address){
												setTimeout(()=>{
													uni.reLaunch({
														url: '/pages/index/index'
													})
												},500)
											}else{
												uni.reLaunch({
													url: '/pages/my/complete?type=1'
												})
											}
										}
									})
									
									
									
								}else{
									text='修改成功'
								}
								uni.showToast({
									icon: 'none',
									title: text
								})
								// uni.clearStorageSync()
								// setTimeout(() => {
								// 	uni.reLaunch({
								// 		url: '/pages/login/login'
								// 	})
								// }, 1500)
							}
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: '两次输入密码不一致'
						})
					}
				}
				if (this.form.type == 2) {
					console.log(this.form)
					if (this.form.intro == this.form.sex) {
						if (!this.userInfo.pay_pwd) {
							var dates = {
								pwd: this.form.intro
							}
						} else {
							var dates = {
								pwd: this.form.name,
								new_pwd: this.form.sex,
							}
						}
						setPayPwd(dates).then(res => {
							if (res.data.code == 1) {
								uni.showToast({
									icon: 'none',
									title: this.userInfo.pay_pwd ? '修改成功' : '设置成功'
								})
								setTimeout(() => {
									uni.navigateBack({

									})
								}, 1500)
							}
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: '两次输入密码不一致'
						})
					}
				}
			},
			wjmm() {
				uni.navigateTo({
					url: '/pages/set/message/ForgotPassword?type=' + this.form.type
				})
			}
		}
	}
</script>

<style>

</style>
